#include <SFML/Graphics.hpp>

void window(){
    sf::RenderWindow window(sf::VideoMode(640,480), "First window!");

    while(window.isOpen()){
        sf::Event event;
        while(window.pollEvent(event)){
            if(event.type == sf::Event::Closed){
                // Close window button clicked.
                window.close();
            }
        }
        window.clear(sf::Color::Black);
        // Draw here.
        window.display();
    }
}

void rectangle(){
    sf::RenderWindow window(sf::VideoMode(640,480), "Rendering the rectangle.");

    // Creating our shape.
    sf::RectangleShape rectangle(sf::Vector2f(128.0f,128.0f));
    rectangle.setFillColor(sf::Color::Red);
    rectangle.setPosition(320,240);
    rectangle.setOrigin(rectangle.getSize().x / 2, rectangle.getSize().y / 2);

    while(window.isOpen()){
        sf::Event event;
        while(window.pollEvent(event)){
            if(event.type == sf::Event::Closed){
                // Close window button clicked.
                window.close();
            }
        }
        window.clear(sf::Color::Black);
        window.draw(rectangle); // Drawing our shape.
        window.display();
    }
}

void sprite(){
    sf::RenderWindow window(sf::VideoMode(640,480), "Bouncing mushroom.");

    sf::Texture mushroomTexture;
    mushroomTexture.loadFromFile("Mushroom.png");
    sf::Sprite mushroom(mushroomTexture);
    sf::Vector2u size = mushroomTexture.getSize();
    mushroom.setOrigin(size.x / 2, size.y / 2);
    sf::Vector2f increment(0.4f, 0.4f);
    mushroom.setColor(sf::Color(255, 0, 0, 150));

    auto wsize = window.getSize();
    while(window.isOpen()){
        sf::Event event;
        while(window.pollEvent(event)){
            if(event.type == sf::Event::Closed){
                window.close();
            }
        }
        auto mushpos = mushroom.getPosition();
        if((mushpos.x + (size.x / 2) > wsize.x && increment.x > 0)
                || (mushpos.x - (size.x / 2) < 0 && increment.x < 0)){
            // Reverse the direction on X axis.
            increment.x = -increment.x;
        }

        if((mushpos.y + (size.y / 2) > wsize.y && increment.y > 0)
                || (mushpos.y - (size.y / 2) < 0 && increment.y < 0)){
            // Reverse the direction on Y axis.
            increment.y = -increment.y;
        }

        mushroom.setPosition(mushpos + increment);

        window.clear(sf::Color(16,16,16,255)); // Dark gray.
        window.draw(mushroom); // Drawing our sprite.
        window.display();
    }
}

int main(){
    sprite();
    return 0;
}
